package com.yijianonghu.controller.wxwebsite.shoppingcart;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yijianonghu.service.wxwebsite.shoppingcart.ShoppingCartService;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.ControllerUtils;
import com.yijianonghu.util.exception.NoResultReturnedException;
import com.yijianonghu.util.exceptionhandler.ControllerExceptionHandler;
import com.yijianonghu.util.result.MessageCode;
import com.yijianonghu.util.result.Result;

@Controller
@RequestMapping(value=Constants.WXConstants.WX_PREFIX+"shoppingcart/", method=RequestMethod.POST)
public class ShoppingCartController {

	private static final Logger logger=LoggerFactory.getLogger(ShoppingCartController.class);
	
	@Autowired
	private ShoppingCartService shoppingCartService;
	
	@Autowired
	private ControllerExceptionHandler exceptionHandler;
	
	@RequestMapping(value="getShoppingCartCount")
	public @ResponseBody Result getShoppingCartCount(HttpSession session){
		Result result=new Result();		
		result.setSuccess(false);
		Long count=null;
		try {
			count = shoppingCartService.getShoppingCartCount(ControllerUtils.getUserId(session));
		} catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取购物车商品数量失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (Exception e) {
			exceptionHandler.handleException(logger, "异常！获取购物车商品数量失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(count!=null){
			result.setSuccess(true);
			result.setInfor(count);
		}

		return result;
	}
	
	
	@RequestMapping(value="saveOperateShoppingCart")
	public @ResponseBody Result saveOperateShoppingCart(HttpSession session, Integer id, Integer keyType, Integer buyCount){
		Result result=new Result();		
		result.setSuccess(false);
		List<Map<String, Object>> resultList=null;
		try {
			resultList = shoppingCartService.saveOperateShoppingCart(ControllerUtils.getUserId(session), id, keyType, buyCount);
		} catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "购物车操作失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (Exception e) {
			exceptionHandler.handleException(logger, "异常！购物车操作失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(resultList!=null){
			result.setSuccess(true);
			result.setInfor(resultList);
		}

		return result;
	}
	
	@RequestMapping(value="addToShoppingCart")
	public @ResponseBody Result addToShoppingCart(HttpSession session,Integer keyId, Integer buyCount, Integer specId){
		Result result=new Result();		
		result.setSuccess(false);
		MessageCode messageCode=null;
		try {
			messageCode = shoppingCartService.addToShoppingCart(ControllerUtils.getUserId(session), keyId, buyCount, specId);
			if(messageCode==null){
				throw new NoResultReturnedException("加入购物车失败！");
			}
		} catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "加入购物车失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "加入购物车失败！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		}
		if(messageCode!=null){
			result.setMessage(messageCode.getDescription());
			result.setSuccess(MessageCode.COMMON_SUCCESS==messageCode);
		}

		return result;
	}
	
	@RequestMapping(value="listShoppingCart")
	public @ResponseBody Result listShoppingCart(HttpSession session){
		Result result=new Result();		
		result.setSuccess(false);
		Map<Object, Map<String, Object>> map=null;
		try {
			map = shoppingCartService.listShoppingCart(ControllerUtils.getUserId(session));
		} catch (InstantiationException | ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取购物车列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (Exception e) {
			exceptionHandler.handleException(logger, "异常！获取购物车列表失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(map==null){
			map=new HashMap<Object, Map<String, Object>>();
		}
		if(result.getError_code()==null){
			result.setSuccess(true);
			result.setInfor(map);
		}
		return result;
	}
	
}
